package com.youedata.modular.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.youedata.modular.entity.GoodsInfo;
import com.youedata.modular.entity.GoodsInfoImgRel;
import com.youedata.modular.mapper.GoodsInfoImgRelMapper;
import com.youedata.modular.mapper.GoodsInfoMapper;
import com.youedata.modular.model.dto.GoodsInfoDTO;
import com.youedata.modular.service.IGoodsInfoImgRelService;
import  com.youedata.modular.service.IGoodsInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youedata.sys.core.util.UserHolder;
import com.youedata.util.ToolUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 商品表 服务实现类
 * </p>
 *
 * @author hst
 * @date 2021-08-05
 */
@Service
@Transactional(rollbackFor = Exception.class)

public class GoodsInfoServiceImpl extends ServiceImpl<GoodsInfoMapper, GoodsInfo> implements IGoodsInfoService {

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    private GoodsInfoMapper goodsInfoMapper;


    @Autowired
    private IGoodsInfoImgRelService goodsInfoImgRelService;

    @Override
    public List queryFuzzySearch(Page page, GoodsInfo goodsInfo) {
        return goodsInfoMapper.queryFuzzySearch(page, goodsInfo);
    }

    @Override
    public List querySort( GoodsInfo goodsInfo) {
        return goodsInfoMapper.querySort(goodsInfo);
    }


    @Override
    public void saveSelective(GoodsInfoDTO goodsInfoDTO) {
        GoodsInfo goodsInfo = new GoodsInfo();
        BeanUtils.copyProperties(goodsInfoDTO, goodsInfo);
        goodsInfo.setCreateUser(Long.valueOf(UserHolder.getUserId()));
        goodsInfo.setCreateTime(LocalDate.from(LocalDateTime.now()));
        goodsInfoMapper.insert(goodsInfo);

        String topImgUrl = goodsInfoDTO.getTopImgUrl();
        if (ToolUtil.isNotEmpty(topImgUrl)) {
            List<GoodsInfoImgRel> goodsInfoImgRels = Lists.newArrayList();
            String[] topImgUrls = topImgUrl.split(",");

            for (String imgUrl : topImgUrls) {
                GoodsInfoImgRel goodsInfoImgRel = new GoodsInfoImgRel();
                goodsInfoImgRel.setGoodsId(goodsInfo.getId());
                goodsInfoImgRel.setTopImgUrl(imgUrl);
                goodsInfoImgRels.add(goodsInfoImgRel);
            }

            goodsInfoImgRelService.saveBatch(goodsInfoImgRels);
        }


    }

    @Override
    public void updateGoodsInfo(GoodsInfoDTO goodsInfoDTO) {
        List<GoodsInfoImgRel> goodsInfoImgRels = Lists.newArrayList();

        GoodsInfo goodsInfo = new GoodsInfo();
        BeanUtils.copyProperties(goodsInfoDTO, goodsInfo);
        goodsInfo.setCreateUser(Long.valueOf(UserHolder.getUserId()));
        goodsInfo.setCreateTime(LocalDate.from(LocalDateTime.now()));
        goodsInfoMapper.updateById(goodsInfo);

        String topImgUrl = goodsInfoDTO.getTopImgUrl();
        if (StringUtils.contains(topImgUrl, ",")) {
            String[] urls = topImgUrl.split(",");
            for (int i = 0; i < urls.length; i++) {
                GoodsInfoImgRel goodsInfoImgRel = new GoodsInfoImgRel();
                goodsInfoImgRel.setGoodsId(goodsInfo.getId());
                goodsInfoImgRel.setTopImgUrl(urls[i]);

                goodsInfoImgRels.add(goodsInfoImgRel);
            }
            this.goodsInfoImgRelService.remove(new LambdaQueryWrapper<GoodsInfoImgRel>().eq(GoodsInfoImgRel::getGoodsId, goodsInfo.getId()));
            this.goodsInfoImgRelService.saveBatch(goodsInfoImgRels);
        }
    }

    @Override
    public GoodsInfo detailById(String id,String countFlag) {
        GoodsInfo goodsInfo = this.getById(id);

        if (StringUtils.equalsIgnoreCase("Y", countFlag)) {
            goodsInfo.setPageview(goodsInfo.getPageview() + 1);
            this.updateById(goodsInfo);
        }


        List<GoodsInfoImgRel> goodsInfoImgRels = this.goodsInfoImgRelService.list(new LambdaQueryWrapper<GoodsInfoImgRel>().eq(GoodsInfoImgRel::getGoodsId, id));
        if (ToolUtil.isNotEmpty(goodsInfoImgRels)) {
//            StringBuffer urls = new StringBuffer();
            StringBuffer urlsFull = new StringBuffer();
            List<String> topImgUrls = Lists.newArrayList();
            goodsInfoImgRels.forEach(x -> {
                if (ToolUtil.isNotEmpty(x.getTopImgUrl())) {
//                    urls.append(x.getTopImgUrl()).append(",");
//                    x.setTopImgUrl(webServerUrl + x.getImageTitle());
                    urlsFull.append(x.getTopImgUrl()).append(",");
                   topImgUrls.add(x.getTopImgUrl());
                }
            });
//            travelInfo.setUrl(urls.toString());
//            travelInfo.setUrlFull(urlsFull.toString());
            goodsInfo.setTopImgUrl(urlsFull.toString());
            goodsInfo.setTopImgUrls(topImgUrls);
        }
        return goodsInfo;
    }
}

